/*
Copyright (C) 2013 Matthew Netsch

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#pragma once

#include "passman.h"
#include "pass.h"

#include <Windows.h>
#include <WinCrypt.h>

namespace PassMan
{
    class DatabaseManagerFile : public IDatabaseManager
    {
    public:
        virtual ~DatabaseManagerFile();

        virtual IDatabase*  Create(const char *name, const char *key);
        virtual IDatabase*  Retrieve(const char *name, const char *key);
        virtual void        Update(IDatabase *database);
        virtual void        Destroy(IDatabase *database);

        DatabaseManagerFile();

    private:
        HCRYPTPROV hProv;
    };

    class Database : public IDatabase
    {
    private:
        char         *name;             ///< Name of database
        char         *key;              ///< Key to lock and unlock database

        int           pass_num;         ///< Number of passwords in list
        int           pass_size;        ///< Password list allocation size (in Pass*)
        Pass        **passes;           ///< Password list

        int           observer_num;     ///< Number of observers in list
        int           observer_size;    ///< Observer list allocation size (in IObserver*)
        IObserver   **observers;        ///< Observer list

        void notify();                  ///< Notifies observers of change in database

    public:
        Database(const char *name, const char *key);
        virtual ~Database();

        virtual IPass* Create(const char *name);
        virtual IPass* Retrieve(const char *name);
        virtual IPass* Retrieve(int index);
        virtual void   Update(IPass *pass);
        virtual void   Destroy(IPass *pass);

        virtual void   setName(const char *name);
        virtual void   setKey(const char *key);
        virtual int    getNumPass();

        virtual void   registerObserver(IObserver *observer);

        virtual IDatabase* clone();

        //Getters
        char* getName();
        char* getKey();
        Pass* getPass(int index);
        Pass* getPass(const char *name);

        bool  addPass(Pass *pass);
        bool  removePass(Pass *pass);
    };
}
